package lium.lbs.lbsim.zfw

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_searchfor_firend.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.BaseActivity
import lium.lbs.lbsim.adapter.SearchForFriendAdapter
import lium.lbs.lbsim.bean.MlistBean
import lium.lbs.lbsim.bean.SearchForFriendBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import java.util.ArrayList
import java.util.HashMap

class SearchForFirendActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {


    lateinit var adapter: SearchForFriendAdapter
    var keywords: String = ""
    var type: Int = 0
    var pageNumber: Int = 1
    private val bfBean = ArrayList<MlistBean>()


    override fun getLayoutId(): Int {
        return R.layout.activity_searchfor_firend
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("查询结果")
        keywords = intent.getStringExtra("keywords")
        type = intent.getIntExtra("type", 0)
        initRecyclerView()
        getDataList()
    }


    fun initRecyclerView() {

        //初始化adapter
        adapter = SearchForFriendAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            pageNumber = 1
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
        refreshLayout.setOnLoadMoreListener {
            ++pageNumber
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }

    }

    fun getDataList() {
        val param = HashMap<String, String>()
        param["keywords"] = keywords
        param["type"] = type.toString()//1：找人；2：找群
        param["pageNumber"] = pageNumber.toString()
        param["pageSize"] = "20"
        postData(
            "/frinds/search",
            param,
            object : DialogCallback<ResponseBean<SearchForFriendBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<SearchForFriendBean>>) {
                    setDataList(response.body().data)
                }
            })
    }

    fun setDataList(list: SearchForFriendBean) {
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        if (list == null || list.mlist.isEmpty()) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
            if (pageNumber > 1) {
                pageNumber--
            }
        } else {
            if (pageNumber == 1) {
                adapter!!.data.clear()
            }
            list?.let {
                adapter!!.data.addAll(list.mlist)
            }
        }
        refreshLayout.finishLoadMoreWithNoMoreData()
        adapter!!.notifyDataSetChanged()
    }

    override fun getNetWorkData() {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent(this, PotentialFriendActivity::class.java)
        intent.putExtra("fid", (adapter!!.data.get(position) as MlistBean).id)
        startActivity(intent)
    }
}
